package leetcode_acm.stackAndQueue;

import java.util.ArrayDeque;

/**
 * 功能描述
 *
 * @author: 张庭杰
 * @date: 2023年03月22日 14:31
 */
public class IsValid {
    public boolean isValid(String ss) {
        char[] s = ss.toCharArray();
        ArrayDeque<Character> deque = new ArrayDeque<>();
        int p = 0;
        while(p < s.length){
            if(s[p] == '(' ||s[p] == '{' || s[p] == '['){
                deque.addFirst(s[p]);
            }else{
                if(deque.isEmpty()){
                    return false;
                }
                char top = deque.removeFirst();
                if(s[p] == ')' && top != '('){
                    return false;
                }else if(s[p] == '}' && top != '{'){
                    return false;
                }else if(s[p] == ']' && top != '['){
                    return false;
                }
            }
            p++;
        }
        return deque.isEmpty();
    }
}
